package org.jeecg.modules.employment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.career.development.entity.GwStudentExt01;
import org.jeecg.modules.career.development.service.IGwStudentExt01Service;
import org.jeecg.modules.employment.entity.GwStudentJobDevelopHistory;
import org.jeecg.modules.employment.entity.GwStudentJobGuide;
import org.jeecg.modules.employment.entity.GwStudentPerfect;
import org.jeecg.modules.employment.entity.SysSettingEntity;
import org.jeecg.modules.employment.service.IGwStudentJobDevelopHistoryService;
import org.jeecg.modules.employment.service.IGwStudentJobGuideService;
import org.jeecg.modules.employment.service.IGwStudentPerfectService;
import org.jeecg.modules.employment.service.ISysSettingService;
import org.jeecgframework.poi.excel.annotation.Excel;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;

@Api(tags = "门户->职业发展")
@RestController
@RequestMapping("/api/home/career/development")
@Slf4j
public class CareerDevelopmentController {

    @Resource
    private IGwStudentExt01Service iGwStudentExt01Service;

    @Resource
    ISysSettingService iSysSettingService;


    /**
     * 就业参数
     *
     * @return 系统设置参数信息
     */
    @ApiOperation(value = "01. 获取学生信息(姓名 学号 院别 专业 年级 开始时间 结束时间 指导教师)")
    @GetMapping(value = "/010")
    public Result<GwStudentExt01> a1QueryPageList() {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String id = sysUser.getId();
        GwStudentExt01 gwStudentExt01 = iGwStudentExt01Service.getById(id);
        return Result.OK(gwStudentExt01);
    }


    @ApiOperation(value = "02. 获取顶岗实习手册模板文件")
    @GetMapping(value = "/020")
    public Result<String> a020() {
        final List<SysSettingEntity> list = iSysSettingService.list();
        for (SysSettingEntity sysSettingEntity : list) {
            if (sysSettingEntity.getSettingKey().equals("顶岗实习手册模板文件")) {
                return Result.OK("", sysSettingEntity.getSettingValue());
            }
        }

        return Result.OK(null);
    }

    @ApiOperation(value = "03. 保存学生 顶岗实习手册文件")
    @PostMapping(value = "/030")
    public Result<Void> a030(Param01 param) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String id = sysUser.getId();
        GwStudentExt01 gwStudentExt01 = iGwStudentExt01Service.getById(id);
        gwStudentExt01.setDgsxHandbookUrl(param.getDgsxHandbookUrl());
        iGwStudentExt01Service.updateById(gwStudentExt01);
        return Result.OK(null);
    }


    @Resource
    IGwStudentJobGuideService iGwStudentJobGuideService;

    @ApiOperation(value = "04. 分页查询大学生就业指导文件")
    @PostMapping(value = "/040")
    public Result<IPage<GwStudentJobGuide>> a040(GwStudentJobGuide param,
                                                 @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                 @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                 HttpServletRequest req) {
        QueryWrapper<GwStudentJobGuide> queryWrapper = QueryGenerator.initQueryWrapper(new GwStudentJobGuide(), req.getParameterMap());
        Page<GwStudentJobGuide> page = new Page<>(pageNo, pageSize);
        IPage<GwStudentJobGuide> pageList = iGwStudentJobGuideService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    @Resource
    IGwStudentJobDevelopHistoryService iGwStudentJobDevelopHistoryService;

    @ApiOperation(value = "05. 获取学生最新的职业发展信息")
    @GetMapping(value = "/050")
    public Result<GwStudentJobDevelopHistory> a050(GwStudentJobDevelopHistory param,
                                                   HttpServletRequest req) {
        GwStudentJobDevelopHistory condition = new GwStudentJobDevelopHistory();
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String id = sysUser.getId();
        condition.setStudentId(id);
        QueryWrapper<GwStudentJobDevelopHistory> queryWrapper = QueryGenerator.initQueryWrapper(condition, req.getParameterMap());
        queryWrapper.orderByDesc("create_time");
        List<GwStudentJobDevelopHistory> pageList = iGwStudentJobDevelopHistoryService.list(queryWrapper);
        if(! pageList.isEmpty()) {
            condition = pageList.get(0);
        }

        condition.setJobDevelopPdfUrl("http://songuo.vip/static/pdf/policy/关于推动职业教育高质量发展的实施方案.pdf");
        return Result.OK(condition);
    }

    @ApiOperation(value = "06. 保存学生最新的职业发展信息")
    @PostMapping(value = "/060")
    public Result<Void> a060(GwStudentJobDevelopHistory param) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String id = sysUser.getId();
        param.setStudentId(id);
        iGwStudentJobDevelopHistoryService.save(param);
        param.setCreateTime(Calendar.getInstance().getTime());
        return Result.OK("保存成功", null);
    }


    @Resource
    private IGwStudentPerfectService gwStudentPerfectService;

    @ApiOperation(value = "07. 分页查询 优秀毕业生")
    @RequestMapping(value = "/070")
    public Result<IPage<GwStudentPerfect>> a070(GwStudentPerfect param,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                HttpServletRequest req) {
        QueryWrapper<GwStudentPerfect> queryWrapper = QueryGenerator.initQueryWrapper(new GwStudentPerfect(), req.getParameterMap());
        Page<GwStudentPerfect> page = new Page<>(pageNo, pageSize);
        IPage<GwStudentPerfect> pageList = gwStudentPerfectService.page(page, queryWrapper);
        return Result.OK(pageList);
    }


    @Data
    public static class Param01 {
        @Excel(name = "顶岗实习手册URL", width = 15)
        @ApiModelProperty(value = "顶岗实习手册URL")
        private java.lang.String dgsxHandbookUrl;
    }

}
